package com.test.java;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 给定一个常数K以及一个单链表L，请编写程序将L中每K个结点反转。
 * 例如：给定L为1→2→3→4→5→6，K为3，则输出应该为3→2→1→6→5→4；
 * 如果K为4，则输出应该为4→3→2→1→5→6，即最后不到K个元素不反转。
 * =============================================================
 * Version          Author          Date            Description
 * 1.0              白建         2017/12/22             创建
 * =============================================================
 */
public class Java_16
{

    private static int N;

    private static int K;

    private static List<Link> L;

    private static String firstNode;

    private static List<Link> links;

    public static void main(String[] args)
    {

        init();
        process();
    }

    private static void init()
    {

        Scanner scanner = new Scanner(System.in);
        String temp = scanner.nextLine();
        String[] strings = temp.split(" ");

        firstNode = strings[0];
        N = Integer.parseInt(strings[1]);
        K = Integer.parseInt(strings[2]);

        L = new ArrayList<>(K);
        links = new ArrayList<>(N);

        Link link;
        for (int i = 0; i < N; i++)
        {
            String line = scanner.nextLine();
            String[] strings1 = line.split(" ");
            link = new Link();
            link.address = strings1[0];
            link.value = Integer.parseInt(strings1[1]);
            link.next = strings1[2];
            links.add(link);
        }
    }

    private static void process()
    {

        String current = firstNode;
        boolean flag = true;
        boolean print = false;
        int i;
        while (flag)
        {
            for (i = 0; i < K; i++)
            {
                Link link = findNode(current);
                L.add(i, link);
                current = link.next;
                if ("-1".equalsIgnoreCase(link.next))
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                for (int j = K - 1; j >= 0; j--)
                {
                    if (print)
                    {
                        System.out.print(L.get(j).address + "\n");
                    }
                    System.out.print(L.get(j).address + " ");
                    System.out.print(L.get(j).value + " ");
                    print = true;
                }
            }
            else
            {
                for (int j = 0; j <= i; j++)
                {
                    System.out.print(L.get(j).address + "\n");
                    System.out.print(L.get(j).address + " ");
                    System.out.print(L.get(j).value + " ");
                }
            }
            L.clear();
        }
        System.out.print("-1");
    }

    private static Link findNode(String current)
    {

        for (int i = 0; i < N; i++)
        {
            if (links.get(i).address.equalsIgnoreCase(current))
            {
                return links.get(i);
            }
        }
        return null;
    }

    static class Link
    {

        public String address;

        public int value;

        public String next;
    }
}
